import java.awt.*;
import java.applet.*;
import java.lang.*;

public class rutherford2 extends Applet implements Runnable
{
int xLoc = 0;

int yLoc = 0;

int xNucl = 112;
int  yNucl = 112;

double forceConst = 100000;

double radius;

double xSpeedInit = 9;
double xSpeed = xSpeedInit;

double ySpeed = 0;

double time=0,delta_time=.01;

int circleSize = 20;
int nuclSize = 100;

int appletSize = 300;

Thread moveCircle=null;

Rectangle clipRect;


public void init()
{
this.setLayout(new BorderLayout());
setBackground(Color.white);
resize(appletSize,appletSize);
clipRect = new Rectangle(0,0,0,0);
}

public void paint(Graphics g)
{
clipRect = new Rectangle(xLoc,yLoc,circleSize,circleSize);
g.setColor(Color.black);
g.fillRect(0,0,appletSize,appletSize);
g.setColor(Color.blue);
g.fillOval(xNucl-nuclSize/2,yNucl-nuclSize/2,xNucl+nuclSize/2,yNucl+nuclSize/2);
g.setColor(Color.red);
g.fillOval(xLoc,yLoc,circleSize,circleSize);
}

public void update(Graphics g)
{
// comment out the next line to try repaint clipping 
g.clipRect(clipRect.x, clipRect.y, clipRect.width + 1, clipRect.height + 1);

paint(g);

}
public void start()
{
if (moveCircle == null)
     {
moveCircle = new Thread(this);
moveCircle.start();
}
}

public void run()
{
while (moveCircle != null)
      {
xLoc += xSpeed;

yLoc += ySpeed;

radius = Math.sqrt((xLoc-xNucl)*(xLoc-xNucl)+(yLoc-yNucl)*(yLoc-yNucl));

if (radius < nuclSize)

{
xSpeed +=forceConst*(xLoc-xNucl)/(nuclSize*nuclSize*nuclSize)*delta_time;
ySpeed +=forceConst*(yLoc-yNucl)/(nuclSize*nuclSize*nuclSize)*delta_time;
}
else

{
xSpeed +=forceConst*(xLoc-xNucl)/(radius*radius*radius)*delta_time;
ySpeed +=forceConst*(yLoc-yNucl)/(radius*radius*radius)*delta_time;
}


if ((xLoc + circleSize) > appletSize ||(xLoc + circleSize) <0) {xLoc=circleSize;
yLoc=(int)Math.round(Math.random()*300);xSpeed=xSpeedInit;ySpeed=0;}
 

if ((yLoc < 0) || ((yLoc + circleSize) > appletSize)) {xLoc=circleSize;
yLoc=(int)Math.round(Math.random()*300);xSpeed=xSpeedInit;ySpeed=0;} 

clipRect = clipRect.union(new Rectangle(xLoc, yLoc, 
circleSize,circleSize));

showStatus("angle turned through = "+(int)( 57.3*Math.atan((double)(-ySpeed/xSpeed))));
 repaint();
 // comment this line out for repaint clipping

//uncomment the next two lines to use repaint clipping
//repaint(clipRect.x, clipRect.y,
//clipRect.width + 1, clipRect.height + 1);

try {Thread.sleep(5);}
catch (InterruptedException e){}
}
}



public void stop()
{
if (moveCircle != null) moveCircle.stop();
moveCircle = null;
}
}

